package service

import (
	"ksd-social-api/modules/special/mapper"
	"ksd-social-api/modules/special/model"
	"ksd-social-api/modules/special/model/context"
)

/**
 * 用户收藏
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
type SpecialUserFavService struct {
}

var specialUserFavMapper = mapper.SpecialUserFavMapper{}

/**
 * 收藏
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 */
func (service *SpecialUserFavService) SaveCancelUserFav(ctx context.FavLikeContext) int64 {
	isFocus := specialUserFavMapper.CountSpecialUserFav(ctx.UserId, ctx.Opid.Uint64(), ctx.SystemId)
	// 收藏
	if isFocus == -1 {
		var specialUserFav model.SpecialUserFav
		specialUserFav.UserId = ctx.UserId
		specialUserFav.SystemId = ctx.SystemId
		specialUserFav.Opid = ctx.Opid.Uint64()
		specialUserFav.Avatar = ctx.Avatar
		specialUserFav.Nickname = ctx.Nickname
		specialUserFav.Username = ctx.Username
		specialUserFav.SystemName = ctx.SystemName
		specialUserFav.Uuid = ctx.Uuid
		specialUserFav.IsFocus = 1
		specialUserFavMapper.SaveSpecialUserFav(ctx.Opid.Uint64(), specialUserFav)
		return 1
	}

	// 再收藏
	if isFocus == 0 {
		specialUserFavMapper.RecoverSpecialUserFav(ctx.UserId, ctx.Opid.Uint64(), ctx.SystemId)
		return 1
	}

	// 取消收藏
	if isFocus == 1 {
		specialUserFavMapper.CancelSpecialUserFav(ctx.UserId, ctx.Opid.Uint64(), ctx.SystemId)
		return 0
	}

	return 0
}

/**
 * 查询是否收藏
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *SpecialUserFavService) GetUserFav(userId uint64, focusId uint64, systemId uint64) int64 {
	return specialUserFavMapper.CountSpecialUserFav(userId, focusId, systemId)
}

/**
 * 删除收藏
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 */
func (service *SpecialUserFavService) DelUserFav(ctx context.FavLikeDelContext) bool {
	return specialUserFavMapper.DelSpecialUserFav(ctx.Id.Uint64(), ctx.UserId, ctx.SpecialId.Uint64(), ctx.SystemId)
}
